library(tidyverse)
library(janitor)
library(lubridate)
library(here)
library(paletteer)
library(tsibble)
library(fable)
library(fabletools)
library(feasts)
library(forecast)
library(sf)
library(tmap)
library(mapview)
us_renew <- read_csv(here("data", "renewables_cons_prod.csv")) %>%
clean_names()
renew_clean <- us_renew %>%
mutate(description = str_to_lower(description)) %>%
filter(str_detect(description, pattern = "consumption")) %>%
filter(!str_detect(description, pattern = "total"))
yyyymm column to a daterenew_date <- renew_clean %>%
mutate(yr_mo_day = lubridate::parse_date_time(yyyymm, "ym")) %>%
mutate(month_sep = yearmonth(yr_mo_day)) %>%
mutate(value = as.numeric(value)) %>%
drop_na(month_sep, value)
# Make a version where month and reat are in separate columns
renew_parsed <- renew_date %>%
mutate(month = month(yr_mo_day, label = TRUE)) %>% # store month in abbreviation, FALSE : month number
mutate(year = year(yr_mo_day))
renew_gg <- ggplot(data = renew_date,
aes(x = month_sep,
y = value,
group = description)) +
geom_line(aes(color = description)) +
labs(x = "Time",
y = "Consumption (Trillion Btu)",
color = "Energy type") +
theme_bw()
# update color with paletteer
renew_gg +
scale_color_paletteer_d("palettetown::electrike")
renew_ts <- as_tsibble(renew_parsed, key = description, index = month_sep)
Let’s look at out ts data in a couple different ways
renew_ts %>% autoplot(value) # know to group by the key, x uses index
renew_ts %>% gg_subseries(value)
renew_ts %>% gg_season(value)
# make season plot use ggplot
ggplot(data = renew_parsed, aes(x = month, y = value, group = year)) +
geom_line(aes(color = year)) +
facet_wrap(~ description,
ncol = 1,
scales = "free",
strip.position = "right")
hydro_ts <- renew_ts %>%
filter(description == "hydroelectric power consumption")
hydro_ts %>% autoplot(value)
hydro_ts %>% gg_subseries(value)
hydro_ts %>% gg_season(value)
hydro_quater <- hydro_ts %>%
index_by(year_qu = ~(yearquarter(.))) %>% # based on the existing group exists
summarize(avg_consumtion = mean(value),
description = description)
dcmp <- hydro_ts %>%
model(STL(value ~ season(window = 5)))
components(dcmp) %>% autoplot()
hist(components(dcmp)$remainder)
hydro_ts %>%
ACF(value) %>%
autoplot()
# each lag is one month
hydro_model <- hydro_ts %>%
model(
ARIMA(value),
ETS(value)
) %>%
fabletools::forecast(h = "4 years") # forecast for the next four years
hydro_model %>% autoplot(filter(hydro_ts, year(month_sep) > 2010))
world <- read_sf(dsn = here("data", "TM_WORLD_BORDERS_SIMPL-0.3-1"),
layer = "TM_WORLD_BORDERS_SIMPL-0.3")
mapview(world)